﻿
Cognitum.Polizas.Carta.Editor = Ext.extend(Karma.EBSimple, {
    canUpdate: false,
    initComponent: function() {
        Ext.apply(this, {
            sections: [{
                name: 'txtEmailTo',
                fieldLabel: 'E-mail(s)',
                id: 'txtEmailTo',
                allowBlank: false                
            }, {
                id: 'txtSubject',
                name: 'txtSubject',
                fieldLabel: 'Asunto',
                allowBlank: false
            }, {
                xtype: 'htmleditor',
                id: 'hteContenido',
                name: 'hteContenido',
                width: 800,
                height: 400,
                hideLabel: true,
                value: this.contenido
}]
            });
            Cognitum.Polizas.Carta.Editor.superclass.initComponent.apply(this, arguments);
            this.doLayout(true);
        },

        doLoad: function() {
        },

        getTBarBaseControls: function() {
            var flagSave = false;
            if (this.isNew) {
                flagSave = !(this.entity.security.New && this.canUpdate);
            }
            else {
                flagSave = !(this.entity.security.Update && this.canUpdate);
            }
            return [
            this.cerrarBtn = new Ext.Button({
                text: 'Cerrar',
                iconCls: 'icon-decline',
                handler: this.doClose,
                scope: this
            })];
        },

        doClose: function() {
            this.fireEvent('beforeclose', true);
        },

        getTBarCustomControls: function() {
            return [{
                xtype: 'button',
                formBind: true,
                text: 'Enviar',
                handler: this.onSend,
                scope: this
            },
            {
                xtype: 'button',
                formBind: true,
                text: 'Imprimir',
                handler: this.onPrint,
                scope: this
}];
            },

            doSubmit: function() {
                if (LOG.isDebugEnabled()) {
                    LOG.debug('[Poliza.CartaForm.onSave]<-');
                    LOG.debug('[Poliza.CartaForm.onSave]--- pol: ' + this.polId);
                    LOG.debug('[Poliza.CartaForm.onSave]--- plant: ' + this.plantId);
                }
                var nombre = this.getForm().findField('txtNombre').getValue();
                var desc = this.getForm().findField('txtDescripcion').getValue();
                var content = this.getForm().findField('hteContenido').getValue();

                this.status.showBusy();
                this.fireEvent('any');
                this.getForm().doAction('action.base', {
                    service: this.entity.service,
                    parameters: [this.entId, nombre, desc, content, this.plantId],
                    success: function(form, action) {
                        var result = action.result.Result;
                        this.status.setStatus({
                            text: 'La carta se guardo exitosamente',
                            clear: true
                        });
                        this.fireEvent('flush');
                        this.status.clearStatus();
                    },
                    failure: function(form, action) {
                        Ext.Msg.alert('Error', action.Result.ErrorMessage);
                        this.status.setStatus({
                            text: 'Ocurrio un error al guardar.',
                            clear: true
                        });
                    },
                    scope: this
                });
            },

            onPrint: function() {
                if (LOG.isDebugEnabled()) {
                    LOG.debug('[Poliza.CartaForm.onPrint]<-');
                }
                LOG.debug('[Poliza.CartaForm.onPrint] - hteContenido' + this.items.first());
                var content = this.getForm().findField('hteContenido').getValue();
                if (LOG.isDebugEnabled()) {
                    LOG.debug('[Poliza.CartaForm.onPrint] - Contenido: ' + content);
                }
                var popup = window.open("preview.html");
                popup.document.write(content);
                popup.print();
                popup.close();
            },

            onSend: function() {
                if (LOG.isDebugEnabled()) {
                    LOG.debug('[Poliza.CartaForm.onSend]<-');
                }
                LOG.debug('[Poliza.CartaForm.onSend] - hteContenido' + this.items.first());
                var content = this.getForm().findField('hteContenido').getValue();
                var to = this.getForm().findField('txtEmailTo').getValue();
                var subject = this.getForm().findField('txtSubject').getValue();
                Ext.Msg.show({
                    title: 'Enviar por e-mail',
                    msg: 'Esta seguro de enviar esta carta por email?',
                    buttons: Ext.Msg.YESNO,
                    fn: function(result) {
                        if (result === 'yes') {
                            this.entity.service = 'Cognitum.Dominio.Polizas.ICartaService';
                            this.doOperationWithParams('DoEnviarPorEmail', [content, to, subject], {
                                fn: function(res) {
                                    var msg = '';
                                    if (res)
                                        msg = 'El e-mail se envió con éxito.';
                                    else
                                        msg = 'Ocurrió un error al enviar el e-mail.';
                                    this.status.setStatus({
                                        text: msg,
                                        iconCls: 'ok-icon',
                                        clear: true
                                    });
                                },
                                scope: this
                            });
                        }
                    },
                    icon: Ext.MessageBox.QUESTION,
                    scope: this
                });

                if (LOG.isDebugEnabled()) {
                    LOG.debug('[Poliza.CartaForm.onSend] ->: ');
                }

            }

        });
Ext.reg('carta.editor', Cognitum.Polizas.Carta.Editor);
